Introduction
Agenda
- Creating Model for Checkbox control
- Creating Controller
- Creating View
- Getting checked values of checkbox in Controller Action Method.
Let's start with creating a simple project in MVC 4 by selecting Basic Project template and naming it “MvcCheckbox”.
After adding the project the following is a folder view of the project that was created.

After adding the project let's add a Model.
Adding Model
To add model just right-click the Models folder then select Add then select Class. An Add New Item dialog will popup asking for the class name. Name it “CheckModel” and click on the Add button.

The following is the simple code that was generated by adding the model.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace MvcCheckbox.Models
- {
- public class CheckModel
- {
- }
- }
Now to add properties to CheckModel.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace MvcCheckbox.Models
- {
- public class CheckModel
- {
- public int Id
- {
- get;
- set;
- }
- public string Name
- {
- get;
- set;
- }
- public bool Checked
- {
- get;
- set;
- }
- }
- }
Adding Controller
To add a new Controller just right-click the Controller folder then select Add then select Controller. An Add Controller dialog will popup asking for the Controller name. Name it “HomeController” and then in the Scaffolding option : Template select “Empty MVC controller“. Finally click on the Add button.

Configuring Controller

The following is the simple Controller code that was generated.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace MvcCheckbox.Controllers
- {
- public class HomeController : Controller
- {
- //
- // GET: /Home/
- public ActionResult Index()
- {
- return View();
- }
- }
- }
Now let's create a list of Checkmodels in the Index Action Method and pass it to the view.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using MvcCheckbox.Models;
- namespace MvcCheckbox.Controllers
- {
- public class HomeController : Controller
- {
- [HttpGet]
- public ActionResult Index()
- {
- var list = new List<CheckModel>
- {
- new CheckModel{Id = 1, Name = "Aquafina", Checked = false},
- new CheckModel{Id = 2, Name = "Mulshi Springs", Checked = false},
- new CheckModel{Id = 3, Name = "Alfa Blue", Checked = false},
- new CheckModel{Id = 4, Name = "Atlas Premium", Checked = false},
- new CheckModel{Id = 5, Name = "Bailley", Checked = false},
- new CheckModel{Id = 6, Name = "Bisleri", Checked = false},
- new CheckModel{Id = 7, Name = "Himalayan", Checked = false},
- new CheckModel{Id = 8, Name = "Cool Valley", Checked = false},
- new CheckModel{Id = 9, Name = "Dew Drops", Checked = false},
- new CheckModel{Id = 10, Name = "Dislaren", Checked = false},
- };
- return View(list);
- }
- }
- }
To add the view just right-click anywhere inside the Action Method Index and select Add.
The Add view dialog will popup with the default View name the same as the Action Method Name.

The following is the Add View dialog.

After seeing the dialog just click on the Add button.
Simple Index.cshtml will be created.

Some default code will be generated after adding the View.
- @{
- ViewBag.Title = "Index";
- }
- <h2>Index</h2>
In this I have created a List of CheckModels and took three controls to display the checkbox.
Note
- HiddenFor: Hiddenfor maintains the ID.
- DisplayFor: DisplayFor displays the checkbox name (text).
- CheckboxFor: CheckboxFor displays the checkbox.
I am iterating these controls to create a unique id for each control.
- @model List<MvcCheckbox.Models.CheckModel>
- @{
- ViewBag.Title = "Index";
- }
- <h2>Index</h2>
- @using (Html.BeginForm())
- {
- for (var i = 0; i < Model.Count(); i++)
- {
- <table>
- <tr>
- <td>
- @Html.HiddenFor(it => it[i].Id)
- @Html.DisplayFor(it => it[i].Name)
- </td>
- <td>
- @Html.CheckBoxFor(it => it[i].Checked,new {Style ="vertical- align:3px}"})
- </td>
- </tr>
- </table>
- }
- <input id="Submit1" type="submit" value="submit" />
- }
To call it just type this URI in your browser window with your localhost port.
Output

Adding New Action Method with Action Selector [HttpPost]
Now let's finally add a new Action Method Post for getting values.
- [HttpPost]
- public ActionResult Index(List<CheckModel> list)
- {
- return View(list);
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using MvcCheckbox.Models;
- namespace MvcCheckbox.Controllers
- {
- public class HomeController : Controller
- {
- [HttpGet]
- public ActionResult Index()
- {
- var list = new List<CheckModel>
- {
- new CheckModel{Id = 1, Name = "Aquafina", Checked = false},
- new CheckModel{Id = 2, Name = "Mulshi Springs", Checked = false},
- new CheckModel{Id = 3, Name = "Alfa Blue", Checked = false},
- new CheckModel{Id = 4, Name = "Atlas Premium", Checked = false},
- new CheckModel{Id = 5, Name = "Bailley", Checked = false},
- new CheckModel{Id = 6, Name = "Bisleri", Checked = false},
- new CheckModel{Id = 7, Name = "Himalayan", Checked = false},
- new CheckModel{Id = 8, Name = "Cool Valley", Checked = false},
- new CheckModel{Id = 9, Name = "Dew Drops", Checked = false},
- new CheckModel{Id = 10, Name = "Dislaren", Checked = false},
- };
- return View(list);
- }
- [HttpPost]
- public ActionResult Index(List<CheckModel> list)
- {
- return View(list);
- }
- }
- }
I have checked the Aquafina , Bisleri and Himalayan values. I should get the value marked true in the Controller.
Output: View with Checked checkboxes

Finally retrieve the values after posting the values from the View to the Controller.
Here I have the values marked as true that I have checked in the View.
I have checked the Aquafina, Bisleri and Himalayan values (0, 5 and 6) that are true and the rest are false.
Output: Values retrieved at Controller [HttpPost] Action selector.

Conclusion
This completes the article on CheckboxFor in MVC4.

Naqi ZaidiPosted Jan 24, 2020, 4:06 PM
How to handle custom validations for checkbox mvc and core mvc
Asanda KhuzwayoPosted Sep 3, 2018, 4:52 AM
How can I get to use the selected values separately maybe for each I use the value for calculation been struggling with t for days now
Maq SaidPosted Jul 3, 2018, 6:16 PM
Saineshwar, how would you write custom validations for checkbox.
john appeasedPosted Nov 24, 2017, 7:11 AM
How do I go about adding more than one list ? for example i have a question and their options
satyesh soniPosted Nov 22, 2017, 1:57 PM
Required annotation is not working in this code.please suggest me what can i do? my email is [email protected]
Manish SethPosted Nov 20, 2017, 7:32 AM
Hi Saineshwar, can you please guide me for how we select only one checkbox from multiple checkbox, how to validate
Nazmul BadshaPosted Oct 15, 2017, 12:44 AM
Nice post my Boss..
moveez romanPosted Jun 30, 2017, 6:39 PM
Why do I see null on Name property after clicking on submit button. I can see there's value in Id and Checked properties but Name got null in it. How can I populate value in Name property.
AttiePosted Mar 10, 2017, 7:18 AM
Thanks good work. How to insert a "Select All" checkbox ? That when you click it, all other checkboxes will be checked.
Manav PandyaPosted Mar 1, 2017, 1:26 AM
I need some help regarding faceted navigation for Products
Manav PandyaPosted Mar 1, 2017, 1:25 AM
Thanks for sharing sir ...
Jorge FilhoPosted Feb 4, 2017, 1:09 AM
Thank you for this article! This helps me a lot.
Anu VPosted Sep 1, 2016, 7:35 AM
Nice...
Rajesh PawdePosted Jun 21, 2016, 1:09 PM
Crystal clear Explanation
tahir karPosted Apr 4, 2016, 6:54 AM
please show an example how to hide/show the textbox using checkbox
Vijendra KumarPosted Jan 19, 2016, 4:37 AM
Sir how to store the value of checkboxlist control in database using entityframework
Ajay KadamPosted Nov 28, 2015, 7:23 AM
nice
Yashwanth MuthineniPosted Aug 27, 2015, 3:16 AM
Nice Share
Murugappan MnPosted Aug 5, 2015, 6:47 AM
Thank u...pls,add how to do it with local database.
Mahesh AllePosted Apr 6, 2015, 1:03 AM
Very helpfull.
Karthik Muthu KaruppanPosted Mar 30, 2015, 11:19 AM
useful
RakeshPosted Mar 27, 2015, 9:27 PM
Helpful
Tom MohanPosted Mar 27, 2015, 7:55 PM
Nice one.